package com.zzx.exception;

import com.zzx.common.exception.ServiceException;
import com.zzx.entity.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    public ApiResult handleServiceException(ServiceException e) {
        log.error("业务异常: {}", e.getMessage());
        return ApiResult.failed(e.getMessage());
    }

    /**
     * 参数校验异常
     */
    @ExceptionHandler({BindException.class, MethodArgumentNotValidException.class})
    public ApiResult handleValidException(Exception e) {
        log.error("参数校验异常: {}", e.getMessage());
        return ApiResult.failed("参数校验失败");
    }

    /**
     * 通用异常
     */
    @ExceptionHandler(Exception.class)
    public ApiResult handleException(Exception e) {
        log.error("系统异常: ", e);
        return ApiResult.failed("系统异常，请联系管理员");
    }
}

